﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using RRRSRoguelike.Enums;
using RRRSRoguelike.Helpers.Actions;

namespace RRRSRoguelike.Managers
{
	/// <summary>
	/// Stores a list of actions along with the keys mapped to those actions
	/// Checks action map against the inputmanager
	/// </summary>
	public class ActionManager
	{
		#region variables
		public ActionMap[] ActionMap;
		public Dictionary<string, Point> DirectionVectors;
		InputManager InputManager = GameManager.Instance.InputManager;
		#endregion

		#region methods
		public void Initialize(int numberOfActions)
		{
			ActionMap = new ActionMap[numberOfActions];
			for (int i = 0; i < numberOfActions; i++)
				ActionMap[i] = new ActionMap();

			SetupDirectionVectors();
		}

		public void SetButtons(Actions Action, ConsoleKey Key)
		{
			ActionMap[(int)Action].ConsoleKeys.Add(Key);
		}

		public bool IsActionPushed(Actions Action)
		{
			for (int i = 0; i < ActionMap[(int)Action].ConsoleKeys.Count(); i++)
			{
				if (InputManager.KeyPush(ActionMap[(int)Action].ConsoleKeys[i]))
				{
					return true;
				}
			}
			return false;
		}

		//public bool IsActionReleased(Actions Action)
		//{
		//    for (int i = 0; i < ActionMaps[(int)Action].ConsoleKeys.Count(); i++)
		//    {
		//        if (InputManager.KeyReleased(ActionMaps[(int)Action].ConsoleKeys[i]))
		//        {
		//            return true;
		//        }
		//    }
		//    return false;
		//}

		//public bool IsActionDown(Actions Action) 
		//{
		//    for (int i = 0; i < ActionMaps[(int)Action].ConsoleKeys.Count(); i++) 
		//    {
		//        if (InputManager.KeyDown(ActionMaps[(int)Action].ConsoleKeys[i]))
		//        {
		//            return true;
		//        }
		//    }
		//    return false;
		//}

		private void SetupDirectionVectors()
		{
			DirectionVectors = new Dictionary<string, Point>
				                   {
					                   {Direction.N.ToString(), new Point(0, -1)},
					                   {Direction.NE.ToString(), new Point(1, -1)},
					                   {Direction.E.ToString(), new Point(1, 0)},
					                   {Direction.SE.ToString(), new Point(1, 1)},
					                   {Direction.S.ToString(), new Point(0, 1)},
					                   {Direction.SW.ToString(), new Point(-1, 1)},
					                   {Direction.W.ToString(), new Point(-1, 0)},
					                   {Direction.NW.ToString(), new Point(-1, -1)}
				                   };
		}


		#endregion

	}

}
